@model EIMS.Models.ViewModels.EmployeeHistoryViewModel

@{
    ViewData["Title"] = "添加历史记录";
}

<div class="container-fluid px-4">
    <h1 class="mt-4">@ViewData["Title"]</h1>
    <ol class="breadcrumb mb-4">
        <li class="breadcrumb-item"><a asp-controller="Home" asp-action="Index">首页</a></li>
        <li class="breadcrumb-item"><a asp-controller="Employees" asp-action="Index">员工管理</a></li>
        <li class="breadcrumb-item"><a asp-controller="Employees" asp-action="Details" asp-route-id="@Model.EmployeeId">员工详情</a></li>
        <li class="breadcrumb-item active">添加历史记录</li>
    </ol>
    
    <div class="card mb-4">
        <div class="card-header">
            <i class="fas fa-history me-1"></i>
            添加员工历史记录
        </div>
        <div class="card-body">
            <div class="row mb-3">
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">员工信息</h5>
                            <p class="card-text">
                                <strong>员工姓名：</strong> @Model.EmployeeName<br />
                                <strong>当前部门：</strong> @Model.CurrentDepartmentName
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            <form asp-action="AddHistory" method="post">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <input type="hidden" asp-for="EmployeeId" />
                <input type="hidden" asp-for="EmployeeName" />
                <input type="hidden" asp-for="CurrentDepartmentId" />
                <input type="hidden" asp-for="CurrentDepartmentName" />
                
                <div class="row mb-3">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label asp-for="Type" class="control-label"></label>
                            <select asp-for="Type" class="form-select" asp-items="ViewBag.HistoryTypes" id="historyType"></select>
                            <span asp-validation-for="Type" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label asp-for="EffectiveDate" class="control-label"></label>
                            <input asp-for="EffectiveDate" class="form-control" type="date" value="@Model.EffectiveDate.ToString("yyyy-MM-dd")" />
                            <span asp-validation-for="EffectiveDate" class="text-danger"></span>
                        </div>
                    </div>
                </div>
                
                <div class="row mb-3" id="departmentSection">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label asp-for="OldDepartmentId" class="control-label"></label>
                            <select asp-for="OldDepartmentId" class="form-select" asp-items="ViewBag.DepartmentId">
                                <option value="">-- 请选择部门 --</option>
                            </select>
                            <span asp-validation-for="OldDepartmentId" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label asp-for="NewDepartmentId" class="control-label"></label>
                            <select asp-for="NewDepartmentId" class="form-select" asp-items="ViewBag.DepartmentId">
                                <option value="">-- 请选择部门 --</option>
                            </select>
                            <span asp-validation-for="NewDepartmentId" class="text-danger"></span>
                        </div>
                    </div>
                </div>
                
                <div class="row mb-3">
                    <div class="col-md-12">
                        <div class="form-group">
                            <label asp-for="Remarks" class="control-label"></label>
                            <textarea asp-for="Remarks" class="form-control" rows="3"></textarea>
                            <span asp-validation-for="Remarks" class="text-danger"></span>
                        </div>
                    </div>
                </div>
                
                <div class="form-group">
                    <a asp-action="Details" asp-route-id="@Model.EmployeeId" class="btn btn-secondary">返回员工详情</a>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save me-1"></i> 保存
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    <script>
        $(document).ready(function() {
            // 根据记录类型调整表单显示
            function toggleDepartmentSection() {
                var type = $("#historyType").val();
                
                // 部门调动时显示部门选择
                if (type == '@((int)EIMS.Models.Enums.HistoryType.Transfer)') {
                    $("#departmentSection").show();
                    // 默认设置原部门为当前部门
                    $("#OldDepartmentId").val('@Model.CurrentDepartmentId');
                }
                // 入职时只显示新部门
                else if (type == '@((int)EIMS.Models.Enums.HistoryType.Hire)') {
                    $("#departmentSection").show();
                    $("#OldDepartmentId").prop('disabled', true);
                    $("#OldDepartmentId").val('');
                    $("#NewDepartmentId").prop('disabled', false);
                }
                // 离职时只显示原部门
                else if (type == '@((int)EIMS.Models.Enums.HistoryType.Termination)') {
                    $("#departmentSection").show();
                    $("#OldDepartmentId").prop('disabled', false);
                    $("#OldDepartmentId").val('@Model.CurrentDepartmentId');
                    $("#NewDepartmentId").prop('disabled', true);
                    $("#NewDepartmentId").val('');
                }
                // 晋升或其他时隐藏部门选择
                else {
                    $("#departmentSection").hide();
                }
            }
            
            // 初始化时检查类型
            toggleDepartmentSection();
            
            // 记录类型改变时检查
            $("#historyType").change(function() {
                toggleDepartmentSection();
            });
        });
    </script>
} 